package sam.learn.framework.security;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import sam.learn.framework.web.ResponseResult;

import java.util.ArrayList;

@RestController
@CrossOrigin
public class JwtAuthenticationController {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private UserDetailsService jwtUserDetailsService;

    @PostMapping(value = "/login")
    public ResponseResult<String> createAuthenticationToken(String username, String password) throws Exception {

        authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password, new ArrayList<>()));

        final String token = jwtTokenUtil.generateTokenWithExpireAfterHour(username, 2);

        return ResponseResult.success("success", token);
    }

}
